-
Notifications
You must be signed in to change notification settings - Fork 76
fix: upsert attachmemts only when attachment exist and handle FileList for react native #1539
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Size Change: +320 B (+0.08%) Total Size: 381 kB
|
@@ -448,7 +448,11 @@ export class AttachmentManager { | |||
}, | |||
}; | |||
|
|||
this.upsertAttachments([failedAttachment]); | |||
const isAttachmentPresent = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@khushal87 we could prevent duplication of these lines by adding a new method updateAttachmet
to AttachmentManager. A proposal of a refactor:
private prepareAttachmentUpdate = (attachmentToUpdate: LocalAttachment) => {
const stateAttachments = this.attachments;
const attachments = [...this.attachments];
const attachmentIndex = this.getAttachmentIndex(attachmentToUpdate.localMetadata.id);
if (attachmentIndex === -1) return null;
const merged = mergeWithDiff<LocalAttachment>(
stateAttachments[attachmentIndex] ?? {},
attachmentToUpdate,
);
const updatesOnMerge = merged.diff && Object.keys(merged.diff.children).length;
if (updatesOnMerge) {
const localAttachment = ensureIsLocalAttachment(merged.result);
if (localAttachment) {
attachments.splice(attachmentIndex, 1, localAttachment);
return attachments;
}
}
return null;
};
updateAttachment = (attachmentToUpdate: LocalAttachment) => {
const updatedAttachments = this.prepareAttachmentUpdate(attachmentToUpdate);
if (updatedAttachments) {
this.state.partialNext({ attachments: updatedAttachments });
}
};
upsertAttachments = (attachmentsToUpsert: LocalAttachment[]) => {
if (!attachmentsToUpsert.length) return;
let attachments = [...this.attachments];
let hasUpdates = false;
attachmentsToUpsert.forEach((attachment) => {
const updatedAttachments = this.prepareAttachmentUpdate(attachment);
if (updatedAttachments) {
attachments = updatedAttachments;
hasUpdates = true;
} else {
const localAttachment = ensureIsLocalAttachment(attachment);
if (localAttachment) {
attachments.push(localAttachment);
hasUpdates = true;
}
}
});
if (hasUpdates) {
this.state.partialNext({ attachments });
}
};
the in uploadAttachment
you would only do this.updateAttachment(failedAttachment)
. WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this can do the trick as well. My bad I was testing upsertAttachments
and thought this would not work but using the updateAttachments
can be useful here and avoid duplication.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have pushed the code and added a few relevant tests. Let me know if its fine to merge it. Thanks 😄
## [9.2.0-offline-support-beta.3](v9.2.0-offline-support-beta.2...v9.2.0-offline-support-beta.3) (2025-05-26) ### Bug Fixes * handle queueing purely offline events while connection still not marked as failed ([39c6848](39c6848)) * not needed watching and some race conditions ([75f4345](75f4345)) * reaction event enrichment but not state update ([110431e](110431e)) * take optionality into account ([5720a61](5720a61)) * upsert attachmemts only when attachment exist and handle FileList for react native ([#1539](#1539)) ([a7f0e02](a7f0e02))
🎉 This PR is included in version 9.2.0-offline-support-beta.3 🎉 The release is available on: Your semantic-release bot 📦🚀 |
## [9.2.0](v9.1.1...v9.2.0) (2025-05-27) ### Bug Fixes * add new poll option only when all of the current options are filled ([#1532](#1532)) ([86cd646](86cd646)) * allow to compose non-empty message from draft without local edits ([#1533](#1533)) ([a704fc3](a704fc3)) * prevent empty poll options and questions in composition ([#1541](#1541)) ([b2e2702](b2e2702)) * respect attachment upload config for files of type FileReference ([#1545](#1545)) ([5f4b551](5f4b551)) * upsert attachmemts only when attachment exist and handle FileList for react native ([#1539](#1539)) ([a7f0e02](a7f0e02)) ### Features * [MOD-513]: Soft/Hard Delete Messages on Ban ([#1538](#1538)) ([5fff918](5fff918)) * add toJSON to ErrorFromResponse class ([#1540](#1540)) ([8f6948f](8f6948f)), closes [GetStream/chat#8211](https://github.com/GetStream/chat/issues/8211) * offline support rework ([#1543](#1543)) ([f84d0f3](f84d0f3)) ### Refactors * general code cleanup (Threads/Polls/MessageComposer) ([#1522](#1522)) ([e4db506](e4db506))
🎉 This PR is included in version 9.2.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
The goal of the PR is to upsert attachments only when they exist. This is especially important because on React Native, we allow cancelling uploading by calling removeAttachments, but when the upload still happens, the upsert proceeds. We will only update you if the attachment is present.
The other fix is that the
FileList
is not supported on RN. Thanks to @MartinCupela for a prompt suggestion on this issue.